﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Globalization;

namespace SIURegistry_Installer
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string [] args)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            
            if (args.Length > 0)
            {
                if (args[0] == "lolishreg")
                {

                    doLoLishRegistryStuff();
                    return;
                }
                String allArgs = "";
                foreach (string a in args)
                {
                    allArgs += "" + a + "|";
                }
               // MessageBox.Show("got some args "+
                 //   allArgs);

                /*
                MessageHelper msg = new MessageHelper();
                int result = 0;
                //First param can be null
                int hWnd = msg.getWindowId(null, "My App Name");
                result = msg.sendWindowsStringMessage(hWnd, 0, “Some_String_Message”);
                //Or for an integer message
                result = msg.sendWindowsMessage(hWnd, MessageHelper.WM_USER, 123, 456);
                  return;*/
                Process process = new Process();
                process.StartInfo.FileName = "Skin Installer Ultimate.exe";
                process.StartInfo.Arguments = string.Concat(new string[]
						{
							" --webArgs \"",
                            allArgs
                            
                            +"\""
						});
                //process.StartInfo.UseShellExecute = false;
                //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //process.StartInfo.CreateNoWindow = true;
                process.StartInfo.WorkingDirectory = Application.StartupPath;
                process.Start();
                process.WaitForExit();
                return;
            }
            doRegistryStuff();
            doLoLishRegistryStuff();
            doYurixyAssociation();
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
        
        static void doRegistryStuff()
        {
            //first delete any old keys (portable fix)               
            try
            {
                Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(
               "SOFTWARE\\Classes\\skininstallerultimatelgg");
            }
            catch// (Exception ex)
            {
                //debugadd("Got expected error trying to delete reg key");
                //Cliver.Message.Inform("Its ok, but \n"+ex.ToString());
            }

            //try to register my key
            Microsoft.Win32.RegistryKey key;
           //Microsoft.Win32.RegistryKey dikey;
            Microsoft.Win32.RegistryKey shellkey;
            key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Classes\\skininstallerultimatelgg");
            key.SetValue("", "URL:LoL Skin Installer LC LGG");
            key.SetValue("URL Protocol", "");
            //dikey = key.CreateSubKey("DefaultIcon");
            //dikey.SetValue("",""+Application.ExecutablePath+",0");
            shellkey = key.CreateSubKey("shell\\open\\command");
            //shellkey.SetValue("","\""+Application.ExecutablePath+"\" --url \"%1\"");
            shellkey.SetValue("","\""+Application.StartupPath+"\\Skin Installer Ultimate.exe"+"\" --url \"%1\"");
            shellkey.Close();
            //dikey.Close();

            key.Close();
        }
        static void doLoLishRegistryStuff()
        {
            //first delete any old keys (portable fix)               
            try
            {
                Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(
               "SOFTWARE\\Classes\\lolsh");
            }
            catch// (Exception ex)
            {
                //debugadd("Got expected error trying to delete reg key");
                //Cliver.Message.Inform("Its ok, but \n"+ex.ToString());
            }

            //try to register my key
            Microsoft.Win32.RegistryKey key;
            //Microsoft.Win32.RegistryKey dikey;
            Microsoft.Win32.RegistryKey shellkey;
            key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Classes\\lolsh");
            key.SetValue("", "URL:LoL Skin Installer LC LGG");
            key.SetValue("URL Protocol", "");
            //dikey = key.CreateSubKey("DefaultIcon");
            //dikey.SetValue("",""+Application.ExecutablePath+",0");
            shellkey = key.CreateSubKey("shell\\open\\command");
            //shellkey.SetValue("","\""+Application.ExecutablePath+"\" --url \"%1\"");
            shellkey.SetValue("", "\"" + Application.StartupPath + "\\Skin Installer Ultimate.exe" + "\" --url \"%1\"");
            shellkey.Close();
            //dikey.Close();

            key.Close();
        }
        static void doYurixyAssociation()
        {


            string yurixyiconPath = "\"" + Application.StartupPath + "\\YuixyIcon.ico\"";
            string lolmodiconPath = "\"" + Application.StartupPath + "\\LoLModIcon.ico\"";
            string[] getRidOf = new string[] { "skininstallerultimatelgg.yurixy",
                "siulgg.yurixyworks", "siulggyurixyworks",
                "skininstallerultimatelggfile.yurixyworks",
                "skininstallerultimatelggfileyurixyworks",
                ".yurixyworks",
                ".yurixyworks2",
                ".yurixyworks5",
            ".lolmod","siulgglolmod"};
            foreach (string regEntry in getRidOf)
            {
                try
                {
                    Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(regEntry);
                }
                catch// (Exception ex)
                {
                    //debugadd("Got expected error trying to delete reg key");
                    //Cliver.Message.Inform("Its ok, but \n"+ex.ToString());
                }
            }
            string[] makeKeysOn = new string[] {".yurixyworks",".yurixyworks2",".yurixyworks5",".lolmod"};
            foreach (string makeKeyOn in makeKeysOn)
            {
                //try to register my key
                Microsoft.Win32.RegistryKey filekey;
                //Microsoft.Win32.RegistryKey dikey;
                Microsoft.Win32.RegistryKey appkey;
                filekey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(makeKeyOn);
                string asocName = "siulgglolmod";
                string iconPath = lolmodiconPath;
                if(makeKeyOn.Contains("yurixy"))
                {
                    asocName = "siulggyurixyworks";
                    iconPath = yurixyiconPath;
                }
                filekey.SetValue("", asocName);
                Microsoft.Win32.RegistryKey fileDefault;
                fileDefault = filekey.CreateSubKey("DefaultIcon");
                fileDefault.SetValue("", iconPath);
                fileDefault.Close();
                filekey.Close();

                appkey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(asocName);
                appkey.SetValue("", "League of Legends Skin");
                appkey.SetValue("DefaultIcon", iconPath);


                Microsoft.Win32.RegistryKey appDefaultKey = appkey.CreateSubKey("DefaultIcon");
                appDefaultKey.SetValue("", iconPath);

                Microsoft.Win32.RegistryKey commandKey = appkey.CreateSubKey("shell\\open\\command");
                commandKey.SetValue("", "\"" + Application.StartupPath + "\\Skin Installer Ultimate.exe" + "\" --file \"%1\"");
                appDefaultKey.Close();
                commandKey.Close();
                appkey.Close();
            }
            ShellNotification.NotifyOfChange();
            
        }
        
    }
    class ShellNotification
    {
        /// <summary>
        /// Notifies the system of an event that an application has performed. An application should use this function if it performs an action that may affect the Shell. 
        /// </summary>
        /// <param name="wEventId">Describes the event that has occurred. The ShellChangeNotificationEvents enum contains a list of options.</param>
        /// <param name="uFlags">Flags that indicate the meaning of the dwItem1 and dwItem2 parameters.</param>
        /// <param name="dwItem1">First event-dependent value.</param>
        /// <param name="dwItem2">Second event-dependent value.</param>
        [DllImport("shell32.dll")]
        private static extern void SHChangeNotify(
            UInt32 wEventId,
            UInt32 uFlags,
            IntPtr dwItem1,
            IntPtr dwItem2);

        /// <summary>
        /// Notify shell of change of file associations.
        /// </summary>
        public static void NotifyOfChange()
        {
            SHChangeNotify(
                (uint)ShellChangeNotificationEvents.SHCNE_ASSOCCHANGED,
                (uint)(ShellChangeNotificationFlags.SHCNF_IDLIST | ShellChangeNotificationFlags.SHCNF_FLUSHNOWAIT),
                IntPtr.Zero,
                IntPtr.Zero);
        }


        [Flags]
        private enum ShellChangeNotificationEvents : uint
        {
            /// <summary>
            /// The name of a nonfolder item has changed. SHCNF_IDLIST or  SHCNF_PATH must be specified in uFlags. dwItem1 contains the  previous PIDL or name of the item. dwItem2 contains the new PIDL or name of the item. 
            /// </summary>
            SHCNE_RENAMEITEM = 0x00000001,
            /// <summary>
            /// A nonfolder item has been created. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item that was created. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_CREATE = 0x00000002,
            /// <summary>
            /// A nonfolder item has been deleted. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item that was deleted. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_DELETE = 0x00000004,
            /// <summary>
            /// A folder has been created. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that was created. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_MKDIR = 0x00000008,
            /// <summary>
            /// A folder has been removed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that was removed. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_RMDIR = 0x00000010,
            /// <summary>
            /// Storage media has been inserted into a drive. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that contains the new media. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_MEDIAINSERTED = 0x00000020,
            /// <summary>
            /// Storage media has been removed from a drive. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive from which the media was removed. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_MEDIAREMOVED = 0x00000040,
            /// <summary>
            /// A drive has been removed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that was removed. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_DRIVEREMOVED = 0x00000080,
            /// <summary>
            /// A drive has been added. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that was added. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_DRIVEADD = 0x00000100,
            /// <summary>
            /// A folder on the local computer is being shared via the network. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that is being shared. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_NETSHARE = 0x00000200,
            /// <summary>
            /// A folder on the local computer is no longer being shared via the network. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that is no longer being shared. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_NETUNSHARE = 0x00000400,
            /// <summary>
            /// The attributes of an item or folder have changed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item or folder that has changed. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_ATTRIBUTES = 0x00000800,
            /// <summary>
            /// The contents of an existing folder have changed, but the folder still exists and has not been renamed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the folder that has changed. dwItem2 is not used and should be NULL. If a folder has been created, deleted, or renamed, use SHCNE_MKDIR, SHCNE_RMDIR, or SHCNE_RENAMEFOLDER, respectively, instead.
            /// </summary>
            SHCNE_UPDATEDIR = 0x00001000,
            /// <summary>
            /// An existing nonfolder item has changed, but the item still exists and has not been renamed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the item that has changed. dwItem2 is not used and should be NULL. If a nonfolder item has been created, deleted, or renamed, use SHCNE_CREATE, SHCNE_DELETE, or SHCNE_RENAMEITEM, respectively, instead.
            /// </summary>
            SHCNE_UPDATEITEM = 0x00002000,
            /// <summary>
            /// The computer has disconnected from a server. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the server from which the computer was disconnected. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_SERVERDISCONNECT = 0x00004000,
            /// <summary>
            /// An image in the system image list has changed. SHCNF_DWORD must be specified in uFlags. dwItem1 contains the index in the system image list that has changed. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_UPDATEIMAGE = 0x00008000,
            /// <summary>
            /// A drive has been added and the Shell should create a new window for the drive. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive that was added. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_DRIVEADDGUI = 0x00010000,
            /// <summary>
            /// The name of a folder has changed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the previous pointer to an item identifier list (PIDL) or name of the folder. dwItem2 contains the new PIDL or name of the folder.
            /// </summary>
            SHCNE_RENAMEFOLDER = 0x00020000,
            /// <summary>
            /// The amount of free space on a drive has changed. SHCNF_IDLIST or SHCNF_PATH must be specified in uFlags. dwItem1 contains the root of the drive on which the free space changed. dwItem2 is not used and should be NULL.
            /// </summary>
            SHCNE_FREESPACE = 0x00040000,
            /// <summary>
            /// Not currently used.
            /// </summary>
            SHCNE_EXTENDED_EVENT = 0x04000000,
            /// <summary>
            /// A file type association has changed. SHCNF_IDLIST must be specified in the uFlags parameter. dwItem1 and dwItem2 are not used and must be NULL.
            /// </summary>
            SHCNE_ASSOCCHANGED = 0x08000000,
            /// <summary>
            /// Specifies a combination of all of the disk event identifiers.
            /// </summary>
            SHCNE_DISKEVENTS = 0x0002381F,
            /// <summary>
            /// Specifies a combination of all of the global event identifiers. 
            /// </summary>
            SHCNE_GLOBALEVENTS = 0x0C0581E0,
            /// <summary>
            /// All events have occurred.
            /// </summary>
            SHCNE_ALLEVENTS = 0x7FFFFFFF,
            /// <summary>
            /// The specified event occurred as a result of a system interrupt. As this value modifies other event values, it cannot be used alone.
            /// </summary>
            SHCNE_INTERRUPT = 0x80000000
        }

        private enum ShellChangeNotificationFlags
        {
            /// <summary>
            /// dwItem1 and dwItem2 are the addresses of ITEMIDLIST structures that represent the item(s) affected by the change. Each ITEMIDLIST must be relative to the desktop folder. 
            /// </summary>
            SHCNF_IDLIST = 0x0000,
            /// <summary>
            /// dwItem1 and dwItem2 are the addresses of null-terminated strings of maximum length MAX_PATH that contain the full path names of the items affected by the change.
            /// </summary>
            SHCNF_PATHA = 0x0001,
            /// <summary>
            /// dwItem1 and dwItem2 are the addresses of null-terminated strings that represent the friendly names of the printer(s) affected by the change.
            /// </summary>
            SHCNF_PRINTERA = 0x0002,
            /// <summary>
            /// The dwItem1 and dwItem2 parameters are DWORD values.
            /// </summary>
            SHCNF_DWORD = 0x0003,
            /// <summary>
            /// like SHCNF_PATHA but unicode string
            /// </summary>
            SHCNF_PATHW = 0x0005,
            /// <summary>
            /// like SHCNF_PRINTERA but unicode string
            /// </summary>
            SHCNF_PRINTERW = 0x0006,
            /// <summary>
            /// 
            /// </summary>
            SHCNF_TYPE = 0x00FF,
            /// <summary>
            /// The function should not return until the notification has been delivered to all affected components. As this flag modifies other data-type flags, it cannot by used by itself.
            /// </summary>
            SHCNF_FLUSH = 0x1000,
            /// <summary>
            /// The function should begin delivering notifications to all affected components but should return as soon as the notification process has begun. As this flag modifies other data-type flags, it cannot by used  by itself.
            /// </summary>
            SHCNF_FLUSHNOWAIT = 0x2000
        }
    }
}
